package com.xxr.mom.ci.common.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.ArrayList;

/**
 * json 工具类
 *
 * @author natsuki_kining
 */
public class JsonUtil {

    public static <T> T parseObject(String text, Class<T> clazz) {
        return JSON.parseObject(text, clazz);
    }

    public static <T> T parseObject(Object data, Class<T> clazz) {
        return JSON.parseObject(toJSONString(data), clazz);
    }

    public static <T> ArrayList<T> parseArrayList(String data, Class<T> clazz) {
        return JSONObject.parseObject(data, new TypeReference<ArrayList<T>>(clazz) {});
    }
    public static <T> ArrayList<T> parseArrayList(Object data, Class<T> clazz) {
        return JSONObject.parseObject(toJSONString(data), new TypeReference<ArrayList<T>>(clazz) {});
    }

    public static JSONObject parseObject(String text) {
        return JSON.parseObject(text, JSONObject.class);
    }

    public static String toJSONString(Object object) {
        return JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect);
    }
}
